<?php

namespace app\controllers;
use yii\web\Controller;
use Yii;
use app\models\wall;


class WallController extends Controller{
    public $enableCsrfValidation = false;
	//信息发布
	public function actionGet(){
		$model = new wall();
		if(Yii::$app->request->isPost){
			$postData = Yii::$app->request->bodyParams;
			$page     = $postData['page'];
	        $start    = $page * 10;
	        $data     = $model->find()->select('*')->where('state = :state', [":state"=>'1'])->offset($start)->limit(10)->orderby('id desc')->asArray()->all();
	        if(empty($data)){
	    	    echo json_encode(array('error' => 1,'data' => '已经拉到底啦~'));
	    	}else{
	    	    echo json_encode(array('error' => 0,'data' => $data,));
	        }
        }    
	}

	//信息接收&数据库输入
	public function actionPublish(){
		$model = new wall();
        if( Yii::$app->request->isPost ){
        	$IP = Yii::$app->request->userIP;//获取用户IP
         	$redis = $this->actionRedisCheck($IP);//检查缓存IP
         	if( $redis == 1 || $redis == 0 ){
         		//检查缓存IP        		
         		//输入数据
         		$postData = Yii::$app->request->post();
        	    $result   = $model->add($postData,$IP);
        	    if( $result == 0 ){
        		    $message = array('error' => '0','content' => '提交数据成功');
        		    echo json_encode($message);
        	    }else{
        		    $message = array('error' => '1','content' => '提交数据失败,请@留言,联系公众号~','information' => $result);
             		echo json_encode($message);
        	    }
            }else if( $redis == -1 ){
         	        $message = array('error' => '-1','content' => '两次发表时间是1个小时哦~');
         	        echo json_encode($message);
            }
        }
    }        



	public function actionRedisCheck($IP){
		$redis = Yii::$app->redis->get($IP);//获取键为$IP的值
        	if($redis){
        		$tip = -1;
        		return $tip;
        	}else{
        		Yii::$app->redis->set($IP,time());//设置$IP键值
                YII::$app->redis->expire($IP,60*60);
        		$tip = 1;
        		return $tip;
        	}
	}

	public function actionGetheadurl(){
		$message = array('url' => "http://ww4.sinaimg.cn/large/8c2e9604gw1fajlce6cquj211y0lgna1.jpg");
		echo json_encode($message);
	}
	 

}

